/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.una.scece.servicios;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.una.scece.clases.Curso;
import com.una.scece.control.controlGestor;
import java.util.ArrayList;

/**
 *
 * @author Erick
 */
public class servicioCursos extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String accion = request.getParameter("accion");
            if (accion.equalsIgnoreCase("guardar")) {
                this.guardarCurso(request, response);
            } else if (accion.equalsIgnoreCase("buscar")) {
                this.buscarCurso(request, response);
            } else if (accion.equalsIgnoreCase("consultar")) {
                this.consultarCurso(request, response);
            } else if (accion.equalsIgnoreCase("modificar")) {
                this.actualizarCurso(request, response);
            } else if (accion.equalsIgnoreCase("listar")) {
                this.listarCurso(request, response);
            } else if (accion.equalsIgnoreCase("eliminar")) {
                this.eliminarCurso(request, response);
            } else if (accion.equalsIgnoreCase("asignarCursoEstu")) {
                this.asignarCursoEstudiante(request, response);
            } else if (accion.equalsIgnoreCase("removerCursoEstu")) {
                this.removerCursoEstudiante(request, response);
            }
        } finally {
        }
    }

    protected void guardarCurso(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String codigo = "";
        codigo = request.getParameter("codigoCurso");
        String descripcion = request.getParameter("descripcion");
        String nrc = request.getParameter("nrc");
        String ciclo = request.getParameter("trimestre");
        int anio = Integer.parseInt(request.getParameter("anio"));
        double costo = Double.parseDouble(request.getParameter("costo"));
        Curso curso = new Curso(codigo, descripcion, nrc, costo, ciclo, anio);
        try {
            Thread.sleep(1000);
            controlGestor.guardarCurso(curso);
            response.setStatus(HttpServletResponse.SC_OK);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            out.close();
        }
    }

    protected void eliminarCurso(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/text");
        PrintWriter out = response.getWriter();
        String codigo = request.getParameter("identificacion");
        Curso curso = new Curso();
        curso.setCodigoCurso(codigo);
        try {
            Thread.sleep(1000);
            controlGestor.eliminarCurso(curso);
            response.setStatus(HttpServletResponse.SC_OK);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            out.close();
        }
    }

    protected void actualizarCurso(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/text");
        PrintWriter out = response.getWriter();
        String codigo = request.getParameter("codigoCurso");
        String descripcion = request.getParameter("descripcion");
        String nrc = request.getParameter("nrc");
        String ciclo = request.getParameter("trimestre");
        int anio = Integer.parseInt(request.getParameter("anio"));
        double costo = Double.parseDouble(request.getParameter("costo"));
        Curso curso = new Curso(codigo, descripcion, nrc, costo, ciclo, anio);
        try {
            Thread.sleep(1000);
            controlGestor.actualizarCurso(curso);
            response.setStatus(HttpServletResponse.SC_OK);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            out.close();
        }
    }

    protected void buscarCurso(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String info = "";
        String codigo = request.getParameter("identificacion");
        Curso curso = new Curso();
        curso.setCodigoCurso(codigo);
        try {
            controlGestor.buscarCurso(curso);
            response.setStatus(HttpServletResponse.SC_OK);
            info = "<div class=\"table\"> ";
            info += "<p><strong>Informacion del Curso</strong></p>"
                    + "<table class=\"listing\" cellpadding=\"0\" cellspacing=\"0\">";
            info += "<tr><th>C&oacute;digo de Curso:&nbsp;</th><td width=\"75%\"><span>&nbsp;" + curso.getCodigoCurso() + "</span></td></tr>";
            info += "<tr><th>Nombre de Curso:&nbsp;</th><td>&nbsp;" + curso.getDescripcion() + "</td></tr>";
            info += "<tr><th>NRC:&nbsp;</th><td>&nbsp;" + curso.getNrc() + "</td></tr>";
            info += "<tr><th>Trimestre:&nbsp;</th><td>&nbsp;" + curso.getCiclo() + "</td></tr>";
            info += "<tr><th>A&ntilde;o:&nbsp;</th><td>&nbsp;" + String.valueOf(curso.getAnio()) + "</td></tr>";
            info += "<tr><th>Costo:&nbsp;</th><td>&nbsp;" + String.valueOf(curso.getCosto()) + "</td></tr></table>";
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            out.println(info);
            out.close();
        }
    }

    protected void consultarCurso(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String info = "";
        String codigo = request.getParameter("identificacion");
        Curso curso = new Curso();
        curso.setCodigoCurso(codigo);
        try {
            controlGestor.buscarCurso(curso);
            response.setStatus(HttpServletResponse.SC_OK);
            info = "<div class=\"table\"><form id=\"form2\">";
            info += "<table class=\"listing form\" cellpadding=\"0\" cellspacing=\"0\">";
            info += "<tr><td><strong>C&oacute;digo de Curso:</strong></td><td><input type=\"text\" class=\"text\" name=\"codigoCurso\" id=\"codigoCurso\" value=\"" + curso.getCodigoCurso() + "\"/></td></tr>";
            info += "<tr><td><strong>Nombre de Curso:</strong></td><td><input type=\"text\" class=\"text\" name=\"nombreCurso\" id=\"nombreCurso\"value=\"" + curso.getDescripcion() + "\"/></td></tr>";
            info += "<tr><td><strong>NRC:</strong></td><td><input type=\"text\" class=\"text\" name=\"nrc\" id=\"nrc\" value=\"" + curso.getNrc() + "\"/></td></tr>";
            info += "<tr><td><strong>Trimestre:</strong></td><td><select id=\"trimestre\" name=\"trimestre\">";
            if (curso.getCiclo().equalsIgnoreCase("1 Trimestre")) {
                info += "<option selected=\"selected\">1 Trimestre</option><option>2 Trimestre</option><option>3 Trimestre</option></select>";
            } else if (curso.getCiclo().equalsIgnoreCase("2 Trimestre")) {
                info += "<option>1 Trimestre</option><option selected=\"selected\">2 Trimestre</option><option>3 Trimestre</option></select>";
            } else if (curso.getCiclo().equalsIgnoreCase("3 Trimestre")) {
                info += "<option>1 Trimestre</option><option>2 Trimestre</option><option selected=\"selected\">3 Trimestre</option></select>";
            }
            info += "<tr><td><strong>A&ntilde;o:</strong></td><td><input type=\"text\" class=\"text\" name=\"anio\" id=\"anio\" value=\"" + String.valueOf(curso.getAnio()) + "\"/></td></tr>";
            info += "<tr><td><strong>Costo:</strong></td><td><input type=\"text\" class=\"text\" name=\"costo\" id=\"costo\" value=\"" + String.valueOf(curso.getCosto()) + "\"/></td></tr></table>";
            info += "<div class=boton align=\"right\"><a><input type=\"button\" name=\"guardar\" class=\"boton\" value=\"Guardar\" onclick=\"return modificarCurso();\"/></a><a><input id=\"btnReset\" type=\"reset\" name=\"cancelar\" value=\"Cancelar\" class=\"boton\"/></a></div>";
            info += "</form></div>";
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            out.println(info);
            out.close();
        }
    }

    protected void listarCurso(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String info = "";
        try {
            Thread.sleep(1000);
            ArrayList<Curso> lista = controlGestor.listarCursos();
            if (lista.size() > 0) {
                info = "<div class=\"table\"> ";
                info += "<p><strong>Informacion de los Cursos</strong></p>"
                        + "<table class=\"listing\" cellpadding=\"0\" cellspacing=\"0\">";
                info += "<tr><th>C&oacute;digo de Curso</th><th>Nombre de Curso</th><th>NRC</th><th>Trimestre</th><th>A&ntilde;o</th><th>Costo</th></tr>";
                for (int i = 0; i < lista.size(); i++) {
                    info += "<tr><td>" + lista.get(i).getCodigoCurso() + "</td><td>" + lista.get(i).getDescripcion() + "</td><td>" + lista.get(i).getNrc() + "</td><td>" + lista.get(i).getCiclo() + "</td><td>" + lista.get(i).getAnio() + "</td><td>" + lista.get(i).getCosto() + "</td></tr>";
                }
            } else {
                info = "<p><strong>No existen registros</strong></p>";
                response.setStatus(HttpServletResponse.SC_OK);
            }
        } catch (Exception e) {
            info = "<p><strong>No existen registros</strong></p>";
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        } finally {
            out.println(info);
            out.close();
        }

        out.println(info);
    }

    protected void asignarCursoEstudiante(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String identificacion = request.getParameter("identificacion");
        String codigo = request.getParameter("codigo");
        int anio = Integer.parseInt(request.getParameter("anio"));
        String trimestre = request.getParameter("trimestre");
        String info = "";
        try {
            controlGestor.asignarCursosEstudiantes(identificacion, codigo, trimestre, anio);
            String datos = controlGestor.verCursosEstudiante2(identificacion, anio, trimestre);
            response.setStatus(HttpServletResponse.SC_OK);
            out.println(datos);
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            //out.println(info);
        } finally {
            out.close();
        }
    }

    protected void removerCursoEstudiante(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String info = "";
        try {
            String identificacion = request.getParameter("identificacion");
            String codigo = request.getParameter("codigo");
            int anio = Integer.parseInt(request.getParameter("anio"));
            String trimestre = request.getParameter("trimestre");
            controlGestor.removerCursosEstudiantes(identificacion, codigo, trimestre, anio);
            String datos = controlGestor.verCursosEstudiante2(identificacion, anio, trimestre);
            out.println(datos);
        } catch (Exception e) {
            //info = "<p><strong>El curso ya ha sido agregado.</strong></p>";
            out.println(info);
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
